/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.connection;

import com.core.log.Debug;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 */
public class ReaderThread implements Runnable {

    private static final int THREAD_SLEEP = 10;
    protected IConnectionListenner connectionListener;
    //private Hashtable utf8 = new Hashtable();
    protected BufferedInputStream inputStream;
//    private byte endByte;
    private byte testOnline;
//    private boolean isStopConnect;
    private boolean isRunning;
    private boolean isDisposed;
    private boolean isLostConnecton;
    private Thread readerThread;

    public ReaderThread(IConnectionListenner listener, InputStream in) {
        connectionListener = listener;
        isRunning = true;
        inputStream = new BufferedInputStream(in);
        readerThread = new Thread(this);
        readerThread.start();
//        readerThread.setName(listener.getName());
    }

    public void setThreadName(String name) {
        readerThread.setName(name);
    }

    private void parseDataReceive() throws Exception {
        if (inputStream.available() > 0) {
            int dataLength = this.inputStream.available();
            //TODO: count kb
            byte[] data = new byte[dataLength];
            this.inputStream.read(data, 0, dataLength);

            if (testOnline == 125) {
                int startByte = 0;
                for (int i = 0; i < dataLength; i++) {
                    if (data[i] == 38) {
                        byte[] buffer = new byte[i - startByte];
                        System.arraycopy(data, startByte, buffer, 0, buffer.length);
                        String message = new String(buffer);
                        connectionListener.addQueue(message);
//                        Debug.d("Rec:" + message);
                        System.out.println("Receive from client: " + message);
                        if (i != dataLength - 1) {
                            if (data[i + 1] == 125) {
                                startByte = i + 2;
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(THREAD_SLEEP);
                byte[] b = new byte[1];
                this.inputStream.read(b);
//                Debug.d("b :" + b[0]);
                testOnline = b[0];
                if (b[0] == 0) {
                    isRunning = false;
                }
                parseDataReceive();
            } catch (Exception ex) {
                isRunning = false;
            }
        }
        try {
            diposeObject();
        } catch (IOException ex) {
        }
    }

    private void diposeObject() throws IOException {
        connectionListener.dispose();
        inputStream.close();
        inputStream = null;
    }

    public void stop() {
        isRunning = false;
    }
}
